package arithmetic.thread;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

/**
 * @author jiangfeng on 2022/1/20
 */
public class TestThreadLocal {
    public static void main(String[] args) throws InterruptedException {
        int threads= Runtime.getRuntime().availableProcessors();

        System.out.println("线程数:"+threads);

        threads =2;
        ThreadFactory tf = new ThreadFactoryBuilder().setUncaughtExceptionHandler((t, e) -> {
            System.out.println("多线程处理校验时异常:"+t.getName()+" e"+e.getMessage());
            // log.error("多线程处理校验时异常", e);
            // exceptions.add(new BatchProcessExcelException("入库出错:" + e.getMessage()));
        }).build();

        ThreadPoolExecutor executor = new ThreadPoolExecutor(threads*2, threads * 2, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(2000),tf);

        //ExecutorService executorService = Executors.newFixedThreadPool(4, tf);


        ThreadLocal<Set<String>> threadLocal = new ThreadLocal<>();
        Random r =new Random();
        Runnable run = ( )->{
            String name = Thread.currentThread().getName();
            //String s = UUID.randomUUID().toString();
            String value  = name+ r.nextInt(100);
            System.out.println(value);

            Set<String> strings = threadLocal.get();
            if(strings==null){
                strings = new HashSet<String>();
                System.out.println(name+ "新new了 set");
            }
            //strings = ?:strings;
            strings.add(value);
                /*try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            System.out.println(name+" 有元素:"+String.join(",",strings));
        };
        Thread[] ts ={new Thread(run),new Thread(run),new Thread(run),new Thread(run)};
        //Random r = new Random();

        for (int i = 0; i < 200; i++) {
            int finalI = i;

            int finalI1 = i;
            executor.execute(()->{
                String name = Thread.currentThread().getName();
                System.out.println(name);
                //String s = UUID.randomUUID().toString();
                String value  = name+ r.nextInt(100) + finalI1;

                Set<String> strings = threadLocal.get();
                if(strings==null){
                    strings = new HashSet<String>();
                    System.out.println(name+ "新new了 set");
                }
                strings.add(value);
                /*try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
                System.out.println(name+" 有元素:"+String.join(",",strings));
            });
            TimeUnit.MILLISECONDS.sleep(200);

        }

        long i=1;
        while( (i = executor.getCompletedTaskCount())<200){
            System.out.println("完成任务:"+i);
            TimeUnit.MILLISECONDS.sleep(200);
            executor.shutdown();
        }
        System.out.println("结束了");;
       // executor.shutdown();

       // System.out.println(thread);



    }


}
